package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.Dish;
import com.woniuxy.entity.OrderItem;
import com.woniuxy.entity.Shop;
import com.woniuxy.mapper.DishMapper;
import com.woniuxy.mapper.OrderItemMapper;
import com.woniuxy.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    public List<Dish> getByCondition(Dish dish){
        return dishMapper.getByCondition(dish);
    }

    @Override
    public Shop getShopByDishId(int dishId) {

        return dishMapper.getShopByDishId(dishId);
    }


    public List<Dish> getByOrderId(int id){
        List<Dish> dishs = new ArrayList<>();
        List<OrderItem> orderItems = orderItemMapper.getByOrderId(id);
        orderItems.forEach(p->{
            dishs.add(dishMapper.selectById(p.getDishId()));
        });
        return dishs;
    }

    @Override
    public Dish search(String keyword) {
        return dishMapper.search(keyword);
    }

    @Override
    public void  add(Dish dish) {
        dishMapper.insert(dish);
    }

    @Override
    public void update(Dish dish){
        dishMapper.update(dish);
    }

}

